home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* polarViewDemo.c - Show the effect of changes in the incidence and
- * azimuth paramters in the polarView routine.
- *
- * Left Mousebutton - change azimuth angle
- * Middle Mousebuttonn - change incidence angle
- * Right Mousebutton - change the twist angle
- * F1 key - print help information
- * R key - reset incidence, azimuth, and twist of eye
- * Escape key - exit the program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <math.h>
- #include <stdio.h> /* for printf */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid specialkeys( GLint, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
-
- void resetView( GLvoid );
- void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static char *progname;
- static void *fixedFont;
-
- static GLdouble xStart = 0.0, yStart = 0.0;
-
- static GLfloat near, far, distance, twistAngle, incAngle, azimAngle;
-
- /* action values */
-
- static enum actions { CHANGE_AZIM, CHANGE_INC, CHANGE_TWIST, CHANGE_NONE };
- static GLint action = CHANGE_AZIM;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( (width / 2) + 4, height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- progname = argv[0];
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - combine modeling transformations to \n"
- "view objects as though encased in a glass ball\n\n"
- "Axes: X - red, Y - green, Z - blue\n\n"
- "Left Mousebutton - change azimuth angle\n"
- "Middle Mousebuttonn - change incidence angle\n"
- "Right Mousebutton - change the twist angle\n"
- "R key - reset incidence, azimuth, and twist angles\n"
- "F1 Key - print Help information\n"
- "Escape key - exit the program\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat yellowAmbient[] = { 1.0, 1.0, 0.0, 1.0 };
- GLfloat yellowDiffuse[] = { 1.0, 1.0, 0.0, 1.0 };
- GLfloat whiteSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
-
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- glEnable( GL_DEPTH_TEST );
-
- /* Set up near and far so that ( far - near ) > maxObjectSize, */
- /* and determine the viewing distance (adjust for zooming) */
- near = 2.5;
- far = 7.0;
-
- resetView();
-
- glMaterialfv( GL_FRONT, GL_AMBIENT, yellowAmbient );
- glMaterialfv( GL_FRONT, GL_DIFFUSE, yellowDiffuse );
- glMaterialfv( GL_FRONT, GL_SPECULAR, whiteSpecular );
- glMaterialf( GL_FRONT, GL_SHININESS, 100.0 );
-
- /* Turn on a default light */
- glEnable( GL_LIGHT0 );
-
- /* Set up fonts to use for rendering */
- fixedFont = GLUT_BITMAP_9_BY_15;
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 47.0, aspect, near, far );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'R':
- resetView();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- specialkeys( GLint key, GLint x, GLint y )
- {
- switch (key) {
- case GLUT_KEY_F1: /* print Help */
- printHelp( progname );
- break;
- }
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- if (state == GLUT_DOWN) {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- action = CHANGE_AZIM;
- break;
- case GLUT_MIDDLE_BUTTON:
- action = CHANGE_INC;
- break;
- case GLUT_RIGHT_BUTTON:
- action = CHANGE_TWIST;
- break;
- }
-
- /* Update the saved mouse position */
- xStart = x;
- yStart = y;
- } else {
- action = CHANGE_NONE;
- }
- }
-
- GLvoid
- motion( GLint x, GLint y )
- {
- switch (action) {
- case CHANGE_AZIM:
- /* Adjust the eye position based on the mouse position */
- azimAngle += (GLdouble) (x - xStart);
- break;
- case CHANGE_INC:
- /* Adjust the eye position based on the mouse position */
- incAngle -= (GLdouble) (y - yStart);
- break;
- case CHANGE_TWIST:
- /* Adjust the eye twist based on the mouse position */
- twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
- break;
- default:
- printf("unknown action %d\n", action);
- }
-
- /* Update the stored mouse position for later use */
- xStart = x;
- yStart = y;
-
- glutPostRedisplay();
- }
-
- void
- resetView( GLvoid )
- {
- distance = near + (far - near) / 2.0;
- twistAngle = 0.0; /* rotation of viewing volume (camera) */
- incAngle = 0.0;
- azimAngle = 0.0;
- }
-
- void
- polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
- GLfloat twist)
- {
- glTranslatef( 0.0, 0.0, -distance);
- glRotatef( -twist, 0.0, 0.0, 1.0);
- glRotatef( -incidence, 1.0, 0.0, 0.0);
- glRotatef( -azimuth, 0.0, 0.0, 1.0);
- }
-
- GLvoid
- drawNormal( GLfloat px, GLfloat py, GLfloat pz,
- GLfloat nx, GLfloat ny, GLfloat nz )
- {
- glPushAttrib( GL_CURRENT_BIT | GL_LINE_BIT | GL_ENABLE_BIT );
- glDisable( GL_LIGHTING );
- glColor3f( 1.0, 1.0, 1.0 );
- glLineWidth( 2.5 );
- glPushMatrix();
- glTranslatef( px, py, pz );
- glBegin( GL_LINES );
- glVertex3f( 0.0, 0.0, 0.0 );
- glVertex3f( nx, ny, nz );
- glEnd();
- glPopMatrix();
- glPopAttrib();
- } /* drawNormal */
-
- GLvoid
- renderBitmapString( void *font, char *string )
- {
- int i;
- int len = (int) strlen(string);
- for (i = 0; i < len; i++) {
- glutBitmapCharacter(font, string[i]);
- }
- }
-
- GLvoid
- drawStatus( GLvoid )
- {
- static char a[50];
- GLsizei winWidth, winHeight;
-
- /* save the ModelView matrix */
- glPushMatrix();
-
- /* clear any current modeling or viewing xforms */
- glLoadIdentity();
-
-
- /* save the Projection matrix */
- glMatrixMode( GL_PROJECTION );
- glPushMatrix();
-
- /* make new world space to display status in */
- glLoadIdentity();
- winWidth = glutGet(GLUT_WINDOW_WIDTH);
- winHeight = glutGet(GLUT_WINDOW_HEIGHT);
- glOrtho( -0.5, winWidth-0.5, -0.5, winHeight-0.5, -1, 1.0 );
-
- glPushAttrib( GL_DEPTH_BUFFER_BIT );
-
- /* don't want text to be hidden */
- glDisable( GL_DEPTH_TEST );
-
- glColor3f( 1.0, 1.0, 0.0 );
-
- glRasterPos3f( 10.0, winHeight-15.0, 0.0 );
- sprintf (a, "left mouse - change azimuth");
- renderBitmapString( fixedFont, a );
-
- glRasterPos3f( 10.0, winHeight-35.0, 0.0 );
- sprintf (a, "middle mouse - change incidence");
- renderBitmapString( fixedFont, a );
-
- glRasterPos3f( 10.0, winHeight-55.0, 0.0 );
- sprintf (a, "right mouse - change twist");
- renderBitmapString( fixedFont, a );
-
- glRasterPos3f( 10.0, winHeight-75.0, 0.0 );
- sprintf (a, "R key - reset view");
- renderBitmapString( fixedFont, a );
-
- /* draw at pixel coord 10, 65 */
- glRasterPos3f( 10.0, 65.0, 0.0 );
- sprintf (a, "azimuth is %4d", (GLint) azimAngle);
- renderBitmapString( fixedFont, a );
-
- glRasterPos3f( 10.0, 40.0, 0.0 );
- sprintf (a, "incidence is %4d", (GLint) incAngle);
- renderBitmapString( fixedFont, a );
-
- glRasterPos3f( 10.0, 15.0, 0.0 );
- sprintf (a, "Twist is %4d", (GLint) twistAngle);
- renderBitmapString( fixedFont, a );
-
- glPopAttrib();
-
- /* restore matrixes */
- glPopMatrix();
-
- glMatrixMode( GL_MODELVIEW );
- glPopMatrix();
- } /* drawStatus */
-
- GLvoid
- drawScene( GLvoid )
- {
- GLfloat lightPosition0[] = { 1.0, 1.0, 1.0, 0.0 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
-
- glLightfv( GL_LIGHT0, GL_POSITION, lightPosition0 );
-
- polarView( distance, azimAngle, incAngle, twistAngle );
-
- /* Turn on lighting computations */
- glEnable( GL_LIGHTING );
-
- /* Draw a lit torus rotated 60 degrees about the x axis */
- glPushMatrix();
- glRotatef( 60.0, 1.0, 0.0, 0.0 );
- glutSolidTorus( 0.3, 0.6, 15, 15 );
-
- /* Turn off lighting comptations */
- glDisable( GL_LIGHTING );
-
- /* Draw a line ( unlit ) representing the face normal */
- drawNormal( 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 );
- glPopMatrix();
-
- XYZaxes();
-
- glColor3f( 0.3, 0.8, 0.8 );
- glutWireSphere( 1.5, 15, 15 );
-
- /* label north and south poles */
- glColor3f( 1.0, 0.0, 0.0 );
- glRasterPos3f( 0.0, 0.0, -1.6 );
- renderBitmapString( fixedFont, "South Pole" );
-
- /* glColor3f( 1.0, 0.0, 0.0 ); */
- glRasterPos3f( 0.0, 0.0, 1.55 );
- renderBitmapString( fixedFont, "North Pole" );
-
- glPopMatrix();
-
- drawStatus();
-
- glutSwapBuffers();
- }
-